Page({

  /**
   * 页面的初始数据
   */
  data: {
    isid: "1",
    head_choose: [
      { id: 1, name: "匠人榜", active: 'active', content: "国茶匠心指数是本平台通过自动捕捉和整合全网平台数据、利用人工智能算法及标准公式，从专业技能、传播能力和社会贡献等多因子方面对匠人进行量化评估后得出的综合指数。" },
      { id: 2, name: "品牌榜", active: '', content: "国茶品牌指数是本平台通过自动捕捉和整合全网平台数据、利用人工智能算法及标准公式，从品牌的知名度、社会认可度和客户忠诚度等多因子对品牌进行量化评估后得出的品牌商业价值综合指数。", blr: " blr" },
      {
        id: 3, name: "珍品榜", active: '', content: "国茶珍品评价是本平台通过整合全网各类别茶业珍品数据和国茶大数据评审专家的评价结果，从茶品的知名度、好评度和传奇度等多个因子方面，利用人工智能算法对茶品价值进行量化评估后得到的综合性评价。"
      }
    ],
    setexplain: "国茶匠心指数是本平台通过自动捕捉和整合全网平台数据、利用人工智能算法及标准公式，从专业技能、传播能力和社会贡献等多因子方面对匠人进行量化评估后得出的综合指数。",
    b_contet: [

    ],
    hihden_box: "block",
    content_box_exp: 'block',
    tid:"1",
    hid:"1",
    sid:"1",
    winW:'',
    nulldata:'none',//有数据不展示。无数据展开
    bton:'无',
  },
  chooseTap: function (e) {
    // 茶类tID
    var tid = this.data.tid;
    // 区域hid
    var hid = this.data.hid;
    // 商品sID
    var sid = e.target.dataset.id;
    var ins = e.target.dataset.index;
    // 榜单ID
    var id = this.data.isid;
    var bgColor = this.data.hihden_box == 'block' ? 'none' : 'block';
    var c_box_content = this.data.content_box_exp == 'none' ? 'block' : 'none';
    // 设置背景颜色数据
    this.setData({
      hihden_box: bgColor,
      content_box_exp: c_box_content,
    });
    if(!sid){
      wx.showLoading({
        title: '加载中',
        mask: true,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
      var Allot = '../Allot/Allot?id=1&hid=' + hid + '&tid=' + tid
      wx.redirectTo({
        url: Allot,
      })
    }else{
      if (id == 2) {
        var urls = getApp().brand +'?sid=' + sid + '&id=' + id + '&tid=' + tid + '&hid=' + hid + "&ins= " + ins;
      } else if (id == 3) {
        var urls = getApp().Treasures +'?sid=' + sid + '&id=' + id + '&tid=' + tid + '&hid=' + hid + "&ins= " + ins;
      } else {
        var urls = getApp().Craftsman +'?sid=' + sid + '&id=' + id + '&tid=' + tid + '&hid=' + hid + "&ins= " + ins;
      }
    }
    wx.hideLoading()
    wx.navigateTo({
      url: urls,
    })
  },
  locationlw: function () {
    wx.navigateTo({
      url: '../Contact/Contact'
    })
  },
  locationdesc: function () {
    wx.navigateTo({
      url: '../Description/Description'
    })
  },
  jisuanwidth:function(num){
    var j = num + 195;
    return j;
  },
  changechoose: function (e) {
    //  改变榜单
    var that = this;
    var tid = that.data.tid;
    var hid = that.data.hid;
    var id = e.target.dataset.ids;
    that.changeData(id, tid, hid)
  },
  // JS 按照二维数组score值从大道小排序
  li_arr: function (aqiData){
    var result = aqiData.sort(function (a, b) {
      return b['score'] - a['score'];
    });
    return result; 
  },
    // 改变榜单后搜索下面内容和改变榜单样式
  changeData: function (id,tid,hid){
    // console.log(id+','+ tid+','+ hid)
    var that = this;
    var pos = that.data.head_choose;
    for (var index in pos) {
      if (pos[index].id == id) {
        pos[index].active = 'active';
        var explain = pos[index].content;
      } else {
        pos[index].active = '';
      }
    }
    wx.request({
      url: getApp().AppUrl + 'index.php/api/api/source',
      method: 'GET',
      data: {
        teacase_id: "" + tid,
        teaarea_id: "" + hid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var source = res.data.source;
        if (!source) {
          source = '无';
        }
        that.setData({
          bton: source//参考来源
        })
      }
    })
    wx.request({
      url: getApp().AppUrl + 'index.php/api/api/getBangDan',
      method: 'GET',
      data: {
        teacase_id: "" + tid,
        teaarea_id: "" + hid,
        bangdan_id: "" + id,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var strNum = 10;
        var datas = res.data;
        var min_score = 0;
        var nulldata = 'none';
        var winW = that.data.winW;
        if (datas.length > 0) {
          min_score = that.min_math(datas);
          var max_score = datas[0]['score'];
        } else {
           nulldata = 'block' 
        }
        for (var c in datas) {
          var name = datas[c]['name'];
          if (name.length > strNum){
             var name = name.substring(0,strNum);
             datas[c]['name'] = name + "...";
           }else{
             datas[c]['name'] = name;
           }
          if (datas[c]['bangdan_id'] == 3){
            var score = datas[c]['score'];
            if (score >=95){
              datas[c]['score'] = "A++";
              datas[c]['whitds'] = winW;
            } else if (score < 95 && score>=90){
              datas[c]['score'] = "A+";
              datas[c]['whitds'] = winW-25;
            } else if (score < 90 && score >=85) {
              datas[c]['score'] = "A";
              datas[c]['whitds'] = winW-50;
            } else if (score < 85 && score >79) {
              datas[c]['score'] = "A-";
              datas[c]['whitds'] = winW-75;
            } else {
              datas[c]['score'] = "B";
              datas[c]['whitds'] =183;
            }
          }else{
            datas = that.li_arr(datas);

              if(datas[c].score){
                var num = (max_score - datas[c].score)/150;
                if(winW - num < 215){
                  datas[c]['whitds'] = 215;
                }else{
                  datas[c]['whitds'] = winW - num; 
                }
                if (datas[c]['score'] == min_score && datas[c]['score']<1000) {
                  datas[c]['whitds'] = 215;
                }
              } else {
                  datas[c]['whitds'] = parseInt(datas[c].score) + 195;
              }
          }
        }
        that.setData({
          b_contet: datas,
          head_choose: pos,
          hid: hid,
          tid: tid,
          isid: id,
          setexplain: explain + "",
          hihden_box: 'block',
          content_box_exp: 'none',
          nulldata: nulldata,
        })
      }
    })
  },

 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var hid = options.hid;
    var tid = options.tid;
    var id = options.id;
    var pos = this.data.head_choose;
    wx.getSystemInfo({
      success: function (res) {
        var winW = (res.windowWidth)*83/100;
        that.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
          winW: winW
        })
      }
    });
    for (var index in pos) {
      if (pos[index].id == id) {
        pos[index].active = 'active';
        var explain = pos[index].content;
      } else {
        pos[index].active = '';
      }
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    wx.request({
      url: getApp().AppUrl + 'index.php/api/api/serach_tea',
      method: 'GET',
      data: {
        teacase_id: "" + tid,
        teaarea_id: "" + hid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res)
        wx.setNavigationBarTitle({
          title: res.data//页面标题为路由参数
        })
      }
      })
    wx.request({
      url: getApp().AppUrl + 'index.php/api/api/source',
      method: 'GET',
      data: {
        teacase_id: "" + tid,
        teaarea_id: "" + hid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        var source = res.data.source;
        if (!source){
          source = '无';
        }
        that.setData({
          bton: source//参考来源
        })
      }
    })
    wx.request({
      url: getApp().AppUrl + 'index.php/api/api/getBangDan',
      method: 'GET',
      data: {
        teacase_id: "" + tid,
        teaarea_id: "" + hid,
        bangdan_id: "" + id,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading()
        var datas = res.data;
        var min_score = 0;
        var winW = that.data.winW;
        if(datas.length>0){
          min_score = that.min_math(datas);
          var max_score = datas[0]['score'];
        } else {
          var nulldata = 'block'
        }
        // 未完成按比例排长度
        for(var c in datas){
          if (datas[c]['bangdan_id'] == 3) {
            var score = datas[c]['score'];
            if (score >= 95) {
              datas[c]['score'] = "A++";
              datas[c]['whitds'] = winW;
            } else if (score < 95 && score >= 90) {
              datas[c]['score'] = "A+";
              datas[c]['whitds'] = winW - 25;
            } else if (score < 90 && score >= 85) {
              datas[c]['score'] = "A";
              datas[c]['whitds'] = winW - 50;
            } else if (score < 85 && score > 79) {
              datas[c]['score'] = "A-";
              datas[c]['whitds'] = winW - 75;
            } else {
              datas[c]['score'] = "B";
              datas[c]['whitds'] = 183;
            }
          } else {
            datas = that.li_arr(datas);
            if (datas[c].score) {
              var num = (max_score - datas[c].score) / 150;
              if (winW - num < 215) {
                datas[c]['whitds'] = 215;
              } else {
                datas[c]['whitds'] = winW - num;
              }
              if (datas[c]['score'] == min_score && datas[c]['score'] <1000) {
                datas[c]['whitds'] = 215;
              }
            } else {
              datas[c]['whitds'] = parseInt(datas[c].score) + 195;
            }
          }
        }
        that.setData({
          setexplain: "" + explain,
          isid: id,
          b_contet: datas,
          head_choose: pos,
          hid: hid,
          tid: tid,
          nulldata: nulldata
        })
      }
    })
  },

  min_math:function(data){
    var minData = data[0]['score'];
    for (var i = 0; i < data.length; i++) {
      if (data[i]['score'] < minData) {
        minData = data[i]['score'];  // 最小值
      }
    }
    return minData;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})